package com.wjy.game.viewer;

import javax.annotation.Resource;
import javax.swing.*;

import com.wjy.game.handler.Handler;
import com.wjy.game.message.Request;
import com.wjy.game.message.Response;
import com.wjy.game.message.data.req.LoginReq;
import com.wjy.game.message.data.res.LoginRes;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import lombok.Data;

import java.awt.*;

/**
 * @author wjy
 * @date 2025/5/20 11:24
 * @description 登录视图
 */
@Data
@Slf4j
@Component
public class LoginViewer extends ClientViewer {

    private JButton settingButton = new JButton("设置");

    private JLabel countLabel = new JLabel("账  号 ");

    private JTextField countField = new JTextField(10);

    private JLabel passwordLabel = new JLabel("密  码 ");

    private JPasswordField passwordField = new JPasswordField(15);

    private JLabel errorLabel = new JLabel();

    private JButton loginButton = new JButton("登  录");

    private JButton registerButton = new JButton("注  册");

    @Resource
    private RegisterViewer registerViewer;

    @Resource
    private ServerSettingViewer settingViewer;

    @Resource
    private IndexViewer indexViewer;

    @Override
    protected void repaintUI() {
        settingButton.setBounds(200, 10, 80, 20);
        add(settingButton);

        countLabel.setBounds(50, 50, 50, 30);
        add(countLabel);
        countField.setBounds(100, 50, 150, 30);
        add(countField);

        passwordLabel.setBounds(50, 100, 50, 30);
        add(passwordLabel);
        passwordField.setBounds(100, 100, 150, 30);
        add(passwordField);

        errorLabel.setBounds(25, 200, 250, 30);
        errorLabel.setForeground(Color.RED);
        add(errorLabel);

        loginButton.setBounds(25, 250, 250, 30);
        add(loginButton);
        registerButton.setBounds(195, 290, 80, 30);
        add(registerButton);

    }

    @Override
    protected void actions() {
        registerButton.addActionListener(e -> {
            this.setVisible(false);
            registerViewer.initUI();
        });
        settingButton.addActionListener(e -> {
            this.setVisible(false);
            settingViewer.initUI();
        });
        loginButton.addActionListener(e -> {
            LoginReq login = new LoginReq();
            login.setPlayerId(countField.getText());
            login.setPassword(new String(passwordField.getPassword()));
            Request<LoginReq> request = Request.create(login);

            getConnectService().request(request);
            getConnectService().registerHandler(request.getTraceId(), new Handler<LoginRes>() {

                @Override
                public void handle(Response<LoginRes> response) {
                    if (response.isSuccess()) {
                        log.info("登录成功");
                        errorLabel.setText("");
                    } else {
                        log.error("登录失败，失败原因：{}", response.getMsg());
                        errorLabel.setText(response.getMsg());
                    }
                    setVisible(false);
                    indexViewer.initUI();
                }
            });
        });
    }

    @Override
    public int getWidth() {
        return 300;
    }

    @Override
    public int getHeight() {
        return 400;
    }
}
